//
//  HELSearchResultVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/2/20.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELSearchResultVC.h"
#import "BTSearchBar.h"
#import "HELCourseCell.h"
#import "SearchTool.h"
#import "NSString+Utilities.h"
#import "HELFilterheader.h"
#import "HELCoursePicker.h"
#import "KLCPopup.h"
#import "Masonry.h"
#import "HELCourseListModel.h"
#import "NHCourseDetailVC.h"
#import "NSString+Utilities.h"
#import "MMComBoBoxView.h"
#import "MMSelectedPath.h"
@interface HELSearchResultVC ()<UISearchBarDelegate,MMComBoBoxViewDelegate,MMComBoBoxViewDataSource>
@property (nonatomic,  strong) BTSearchBar *searchBar;



//选状态
//@property (nonatomic, assign) HELScoreType scoreType;
////排序
//@property (nonatomic, assign) HELSortType sortType;
//
//@property (nonatomic, assign) HELCateType cateType;
@property (nonatomic,  strong) MMComBoBoxView *comBoBoxView;

@property (nonatomic,  strong) NSString *keyword;

//选状态
@property (nonatomic, strong) NSString *scoreType;
//排序
@property (nonatomic, assign) NSInteger sortType;

@property (nonatomic,  strong) NSMutableArray *itemArray;


@end

@implementation HELSearchResultVC
- (NSMutableArray *)itemArray{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
    
}

- (instancetype)init
{
    self = [super init];
    if (self) {
       
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
       [self addFilterView];
}


- (BTSearchBar *)searchBar{
    if (!_searchBar) {
        _searchBar = [BTSearchBar searchBarWithDelegate:self];
        
        
    }
    return _searchBar;
}

- (instancetype)initWithKeyWord:(NSString *)keyWord
{
    self = [super init];
    if (self) {
        self.searchBar.text = keyWord;
        self.keyword = keyWord;
        self.scoreType = nil;
        self.sortType = -1;  //
    }
    return self;
}
- (void)configUI{

    self.navigationItem.titleView = self.searchBar;
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:0 target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem = right;
    
   

     self.tableView.frame = CGRectMake(0, 44, kScreenWidth, kScreenHeight - 64-44);
    [self.tableView registerNib:[UINib nibWithNibName:@"HELCourseCell" bundle:nil] forCellReuseIdentifier:@"HELCourseCell"];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.comBoBoxView];
 
    
    self.dataArray =[NSMutableArray array];
  
    weakSelf_define;
    [self.tableView wsj_HeaderRefreshBlock:^{
        [weakSelf searchRequest:YES];
    }];
    [self.tableView wsj_FooterRefreshBlock:^{
         [weakSelf searchRequest:NO];
    }];
    [self searchRequest:YES];

}
- (MMComBoBoxView *)comBoBoxView{
    if (!_comBoBoxView) {
        _comBoBoxView = [[MMComBoBoxView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
        _comBoBoxView.dataSource = self;
        _comBoBoxView.delegate = self;
    }
    return _comBoBoxView;
}
- (void)addFilterView{
    
    MMItem *rootItem1 = [MMItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:@"学分"];
    
    
    MMItem *rootItem1_0 =[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:@"全部"];
    rootItem1_0.code = nil;
    
    MMItem *rootItem1_1 =[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:@"I 类"];
    rootItem1_1.code = @"一类";
    
    MMItem *rootItem1_2 =[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:@"II 类"];
    rootItem1_2.code = @"二类";
    
    
    [rootItem1 addNode:rootItem1_0];
    [rootItem1 addNode:rootItem1_1];
    [rootItem1 addNode:rootItem1_2];
    
    
    //second root
    MMItem *rootItem2 = [MMItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:@"默认"];
    //first floor
    
    MMItem *rootItem2_0 =[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"默认"]];
        rootItem2_0.code =[@(-1) stringValue];
    MMItem *rootItem2_1 =[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"最新"]];
    rootItem2_1.code = [@(2) stringValue];
    
    MMItem *rootItem2_2 =[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"热门"]];
    rootItem2_2.code = [@(1) stringValue];
    
    MMItem *rootItem2_3 =[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:[NSString stringWithFormat:@"好评"]];
    rootItem2_3.code = [@(3) stringValue];
    
       [rootItem2 addNode:rootItem2_0];
     [rootItem2 addNode:rootItem2_1];
    [rootItem2 addNode:rootItem2_2];
   
    
    [rootItem2 addNode:rootItem2_3];
    
    [self.itemArray addObject:rootItem1];
    [self.itemArray addObject:rootItem2];
    
    [self.view addSubview:self.comBoBoxView];
    [self.comBoBoxView reload];
    
    
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [self.comBoBoxView dimissPopView];
}


- (void)cancel{
    
    [self.navigationController popToRootViewControllerAnimated:YES];

}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    self.keyword = [searchBar.text trim];
    [self searchRequest:YES];
}


- (void)searchRequest:(BOOL)isRecover{
    
    static NSUInteger page = 1;
    if (isRecover) {
        page =1;
    }else{
        page ++;
    }
    [self.searchBar resignFirstResponder];
    if (!self.searchBar.text) {
        cwqAlertMessage(@"关键字不能为空!");
        return;
    }
    [SearchTool addASearchKeyWord:[self.searchBar.text trim]];
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"page"] = @(page);
    params[@"rows"] = @(15);
    
    if (self.sortType>=0) {
        
        params[@"orderType"] = @(self.sortType);
    }
    if (self.scoreType) {
        params[@"credit"] = self.scoreType;
    }

    if (self.keyword) {
        
        params[@"courseName"] = self.keyword;
    }
    weakSelf_define;
    self.tableView.loading = YES;
    [[HELNetworkManager sharedMg] POST:helCourseSearchUrl parameters:params success:^(id responseObject) {
        
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
            
            if (isRecover) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSInteger totalPage = 0;
            if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]]) {
                
            }else{
                totalPage =[responseObject[@"data"][@"totalPage"] integerValue];
            }
            
            NSInteger currentPage = [responseObject[@"data"][@"page"] integerValue];
            if (currentPage>=totalPage) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer resetNoMoreData];
            }
            
            [weakSelf.dataArray addObjectsFromArray:[HELCourseListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
            [weakSelf.comBoBoxView showResultWithString:responseObject[@"data"][@"total"]];
            [weakSelf.tableView reloadData];

        }
        
        
    } failure:^(NSString *err) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
    }];
    
    
    
   
   
 
}

#pragma mark - MMComBoBoxViewDataSource
- (NSUInteger)numberOfColumnsIncomBoBoxView :(MMComBoBoxView *)comBoBoxView {
    return self.itemArray.count;
}
- (MMItem *)comBoBoxView:(MMComBoBoxView *)comBoBoxView infomationForColumn:(NSUInteger)column {
    return self.itemArray[column];
}


- (CGFloat)widthOfeachItemOfcomBoBoxView:(MMComBoBoxView *)comBoBoxViewd{
    return 60;
}
#pragma mark - MMComBoBoxViewDelegate


- (void)comBoBoxView:(MMComBoBoxView *)comBoBoxViewd didSelectedItemsPackagingInArray:(NSArray *)array atIndex:(NSUInteger)index {
    MMItem *rootItem = self.itemArray[index];
    MMSelectedPath *path = array.firstObject;
    MMItem *seletItem = rootItem.childrenNodes[path.firstPath];
    //    path.firstPath
    if (index == 1) { //排序
        
        self.sortType = [seletItem.code integerValue];
    }else{
        self.scoreType =seletItem.code;
    }
    [self searchRequest:YES];
    
}

- (void)viewWillAppear:(BOOL)animated{

    [self.comBoBoxView dimissPopView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    HELCourseCell *cell =[tableView dequeueReusableCellWithIdentifier:@"HELCourseCell"];
    cell.courseListModel = self.dataArray[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HELCourseListModel *model = self.dataArray[indexPath.row];
    NHCourseDetailVC *vc =[NHCourseDetailVC new];
    vc.courseId = [model.id integerValue];
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.searchBar resignFirstResponder];
}
@end
